package com.lapelittle.framework.service;

import com.lapelittle.framework.entity.SysUser;
import com.lapelittle.framework.mapper.SysUserMapper;
import com.lapelittle.framework.model.RegisterRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class RegisterService {
    @Autowired
    private SysUserMapper userMapper;
    
    public boolean register(RegisterRequest request) {
        // 验证两次密码是否一致
        if (!request.getPassword().equals(request.getConfirmPassword())) {
            throw new RuntimeException("两次输入的密码不一致");
        }
        
        // 检查用户名是否已存在
        if (userMapper.findByUsername(request.getUsername()) != null) {
            throw new RuntimeException("用户名已存在");
        }
        
        // 创建新用户
        SysUser user = new SysUser();
        user.setUsername(request.getUsername());
        // 密码MD5加密
        user.setPassword(DigestUtils.md5DigestAsHex(request.getPassword().getBytes()));
        
        // 注册用户
        return userMapper.register(user) > 0;
    }
} 